package org.mags.dao.common;

import java.io.Serializable;
import java.util.Collection;

public class RestrictionAttribute implements Serializable {
	private RestrictionOperator operator;
	private Object value;
	private String name;
	
	private String getStringValue() {
		if(value==null)return "null";
		if(value instanceof Object[]
		|| value instanceof Collection) {
			Object[] array = new Object[0];
			if(value instanceof Object[])
				array = (Object[])value;
			else if(value instanceof Collection)
				array = ((Collection<?>)value).toArray();
			String arrayString = null;
			for(Object v: array) {
				if(v!=null)
					arrayString = arrayString==null?v.toString():(arrayString+", "+v.toString());
			}
			arrayString = arrayString==null?"":arrayString;
			arrayString = "[" + arrayString + "]";
			return arrayString;
		} else
			return value.toString();
	}
	
	@Override
	public String toString() {
		String _name = name;
		if(_name==null)_name = "null";
		String _operator = operator==null?"null":operator.toString();
		return _name+" "+_operator+" "+getStringValue();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public RestrictionOperator getOperator() {
		return operator;
	}

	public void setOperator(RestrictionOperator operator) {
		this.operator = operator;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public RestrictionAttribute() {
	}
	public RestrictionAttribute(String name, Object value) {
		this(name, RestrictionOperator.EQ, value);
	}
	public RestrictionAttribute(String name, RestrictionOperator operator, Object value) {
		this.name = name;
		this.operator = operator;
		this.value = value;
	}
	
}
